﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DatabaseModule
{
    //These methods extend the generic Dictionary class
    public static class DictionaryExt
    {
        //Returns an array of the Dictionary's Keys
        public static T[] KeysToArray<T, U>(this Dictionary<T, U> dict)
        {
            //Create a new array to hold the keys
            T[] keys = new T[dict.Count];

            //Copy the keys to the array
            dict.Keys.CopyTo(keys, 0);

            //Return the array
            return keys;
        }

        //Returns an array of the Dictionary's Values
        public static U[] ValuesToArray<T, U>(this Dictionary<T, U> dict)
        {
            //Create a new array to hold the keys
            U[] values = new U[dict.Count];

            //Copy the keys to the array
            dict.Values.CopyTo(values, 0);

            //Return the array
            return values;
        }
        
        
        //Versions of the above that perform delegation
        public static string[] KeysToArray(this KeyValues dict)
        {
            return dict.Keys.ToArray();
        }

        public static string[] ValuesToArray(this KeyValues dict)
        {
            return dict.Values.ToArray();
        }
        

        //Returns an XML string showing the contents of the dictionary
        public static string ToXml<T, U>(this Dictionary<T, U> dict, string entityName)
        {
            //Create a string to hold the contents of the dictionary
            string xmlStr = "<" + entityName + ">";

            //Loop through the key/value pairs
            foreach (KeyValuePair<T, U> pair in dict)
            {
                //Add the key/value pair to the list of components
                xmlStr += "<" + pair.Key + ">" + pair.Value + "</" + pair.Key + ">";
            }

            //Return the generated string
            return xmlStr + "</" + entityName + ">";
        }

        //Returns a JSON-style string showing the contents of the dictionary
        public static string DumpContents<T, U>(this Dictionary<T, U> dict)
        {
            //Create a string to hold the contents of the dictionary
            string contentsStr = "{\n";

            //Loop through the key/value pairs
            foreach (KeyValuePair<T, U> pair in dict)
            {
                //Add the key/value pair to the list of components
                contentsStr += "\t" + pair.Key + " => '" + pair.Value + "',\n";
            }

            //Return the generated string
            return contentsStr.Substring(0, contentsStr.Length - 2) + "\n}";
        }

        //Loops through a list of dictionaries and builds the JSON-style string for all of them
        public static string DumpContents<T, U>(this List<Dictionary<T, U>> list)
        {
            //Create a string to hold the contents of the array
            string contentsStr = "{\n";

            //Loop through the dictionaries in the list
            foreach (Dictionary<T, U> dict in list)
            {
                contentsStr += "\t" + dict.DumpContents().Replace("\n", "\n\t") + ",\n";
            }

            //Return the generated string
            return contentsStr.Substring(0, contentsStr.Length - 2) + "\n}";
        }

        //Consolidates values from a list of dictionaries based on key
        public static List<U> ConsolidateValues<T, U>(this List<Dictionary<T, U>> list, T key)
        {
            List<U> values = new List<U>();

            foreach (Dictionary<T, U> listItem in list)
            {
                try
                {
                    values.Add(listItem[key]);
                }
                catch (Exception)
                {
                    //Something screwed up, ignore it and get on with our life
                }
            }

            return values;
        }
    }
}
